import 'package:boss/ui/page/unit/page/unitManager/unit_manager_page.dart';
import 'package:boss/ui/page/unit/unit_provider.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/custom_list_title.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:boss/ui/widget/loader/empty_widget.dart';
import 'package:boss/ui/widget/loader/loader.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';

Future<String> showPickerUnit(BuildContext context) {
  return Navigator.push<String>(
    context,
    MaterialPageRoute(
      builder: (context) => PickerUnitPage(),
    ),
  );
}

@RouterConfig(PickerUnitPage.pageName)
class PickerUnitPage extends StatefulWidget {
  static const pageName = '/PickerUnitPage';

  @override
  _PickerUnitPageState createState() => _PickerUnitPageState();
}

class _PickerUnitPageState extends BaseState<PickerUnitPage> {
  _PickerUnitPageState() : super(PickerUnitPage.pageName);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CenterTitleAppBar(
        title: '选择单位',
        actions: [
          LabelButton(
            title: '编辑',
            padding: EdgeInsets.only(left: 8, top: 2, bottom: 2),
            onTap: () {
              Navigator.pushNamed(context, UnitManagerPage.pageName);
            },
          )
        ],
      ),
      body: Loader<List<String>>(
        loadTask: Provider.of<UnitProvider>(context, listen: false).queryUnit,
        builder: (context) => Consumer<UnitProvider>(
          builder: (context, provider, _) {
            return provider.units.isEmpty
                ? EmptyWidget()
                : ListView.builder(
                    padding: EdgeInsets.symmetric(horizontal: 30.w),
                    itemBuilder: (context, index) {
                      if (index == 0) {
                        return CustomListTitle(
                          title: '不选择',
                          onTap: () {
                            Navigator.pop(context, 'cancel');
                          },
                        );
                      }
                      return CustomListTitle(
                        title: provider.units[index - 1],
                        onTap: () {
                          Navigator.pop(context, provider.units[index]);
                        },
                      );
                    },
                    itemCount: provider.units.length + 1,
                  );
          },
        ),
      ),
    );
  }
}
